﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EmailSendFromGoogle
{
    /// <summary>
    /// je mets mes commentaires
    /// </summary>
    public class SniffFiles
    {
        //le repertoire
        public string Directory { get; private set; }
        public bool RealLife { get; private set; }

        public SniffFiles(string source, bool realLife)
        {
            Directory = source;
            RealLife = realLife;
        }
        public void DoEverything()
        {
            var directory = new DirectoryInfo(Directory);
            var result = directory.GetDirectories();

            var listeEmail = result.Select(directoryInfo =>
                                    {
                                        var person = directoryInfo.Name;

                                        var start = person.IndexOf('(') + 1;
                                        var end = person.IndexOf(')');

                                        var matricule = string.Empty;
                                        if (RealLife)
                                            matricule = person.Substring(start, end - start);
                                        else
                                            matricule = "11167878";

                                        var prenom = person.Substring(person.IndexOf(',') + 2, person.IndexOf('(') - person.IndexOf(',') - 2);

                                        var fichierRetroAction = directoryInfo
                                                                .GetDirectories()
                                                                .First(x => x.Name == "Fichiers de rétroaction");
                                        var attachementFullName = fichierRetroAction
                                                                        .EnumerateFiles()
                                                                        .First()
                                                                        .FullName;


                                        return new { matricule = matricule + "@hec.ca", nom = prenom, attachement = attachementFullName };
                                    })
                                    .Select(x => 
                                    {
                                        new Email().RealSend(x.matricule, x.nom, x.attachement);
                                        return x;
                                    })
                                    .ToList();
 
        }
    }
}
